#include "../build/BG_BOTTOM_DAY_1_1.h"
#include "../build/BG_BOTTOM_DAY_1_2.h"
#include "../build/BG_TOP_DAY_DAY_1.h"
#include "../build/BG_TOP_NIGHT_NIGHT_1.h"
#include "../build/LOGO_RITH.h"
#include "../build/PRESS_START.h"
#include "../build/SPLASH_BOTTOM.h"
#include "../build/SPLASH_TOP.h"
#include "../build/MENU.h"
#include "../build/HELP_BOTTOM.h"
#include "../build/HELP_TOP.h"
#include "../build/TROPHIES.h"
#include "../build/CREDITS.h"
#include "../build/ARTWORKS.h"
#include "../build/STAGE_SELECT_1.h"
#include "../build/STAGE_SELECT_2.h"
#include "../build/STAGE_SELECT_3.h"
#include "../build/ARTWORK_1.h"
#include "../build/ARTWORK_2.h"
#include "../build/ARTWORK_3.h"
#include "../build/GAME_OVER_TOP.h"
#include "../build/GAME_OVER_BOTTOM.h"
#include "../build/LEVEL_WON_TOP.h"
#include "../build/LEVEL_WON_BOTTOM.h"
#include "../build/GAME_WON_TOP.h"
#include "../build/GAME_WON_BOTTOM.h"
#include "../build/LEVEL_PERSO_WON_TOP.h"
#include "../build/LEVEL_PERSO_WON_BOTTOM.h"


#include "..\include\BackgroundManager.hpp"
#include "..\include\Game.hpp"
#include "..\include\Application.hpp"
#include "nds.h"
#include "stdio.h"



BackgroundManager::BackgroundManager(void)
{
	loadedBG = NONE;
	bgBottom2 = 0; 
	bgBottom3 = 0;
	bgTop2 = 0;
	bgBottom2X = 0; 
	bgBottom3X = -256;
	bgTop2X = 0; 
	backgroundTicks = 0;
	backgroundSpeed = 0;
	backgroundScroll = false;
	int id = bgInitSub(3, BgType_Bmp16, BgSize_B16_512x256, 0 ,0);//Not used but must be initialized to allow priority set
	bgSetPriority(id, 3);
}


BackgroundManager::~BackgroundManager(void)
{

}

int BackgroundManager::loadBackground(const unsigned int* data,  int length, unsigned int bgNumber, Screen screen, bool scrollFix, Application* app){

	int id = 0;
	int offset= 0;
	if(bgNumber == 2){
		offset =0;
	}
	else if(bgNumber == 3){
		offset =0;
	}

	if(screen == BOTTOM){
		id = bgInit(bgNumber, BgType_Bmp16, BgSize_B16_256x256, offset,0);
		decompress(data, bgGetGfxPtr(id),  LZ77Vram);

		if(app->state == GAME) bgSetPriority(id, 3);
	}

	else if(screen == TOP && scrollFix == false && DEBUG ==0){

		id = bgInitSub(bgNumber, BgType_Bmp16, BgSize_B16_256x256, offset,0);
		decompress(data, bgGetGfxPtr(id),  LZ77Vram);

		if(app->state == GAME) bgSetPriority(id, 3);
		
	}

	else if(screen == TOP && scrollFix == true && DEBUG ==0){

		
		id = bgInitSub(bgNumber, BgType_Bmp16, BgSize_B16_512x256, offset ,0);	
		dmaCopy(data, bgGetGfxPtr(id), length/2 );	

		
		if(app->state == GAME) bgSetPriority(id, 3);
	
	}
	//else std::cerr << "undefined case "<< std::endl;

	//Maj de bgID
	if (screen == BOTTOM && bgNumber == 2) bgBottom2 = id;
	else if  (screen == BOTTOM && bgNumber == 3) bgBottom3 = id;
	else if  (screen == TOP && bgNumber == 2) bgTop2 = id;

	bgUpdate();

	return id;
}

void BackgroundManager::reset(){

	bgBottom2X = 0; 
	bgBottom3X = -256;
	bgTop2X = 0; 
	backgroundTicks = 0;
	backgroundSpeed = 0;
	backgroundScroll = false;

}

void BackgroundManager::displayBackgrounds(AppState state, Application* app){

	switch(state){

	case SPLASH : {

		loadBackground(SPLASH_BOTTOMBitmap, 0,  2, BOTTOM, false, app);
		loadBackground(SPLASH_TOPBitmap, 0, 2, TOP, false, app);
		loadedBG = SPLASH;
		break;
				  }

	case HOME :{

		loadBackground(PRESS_STARTBitmap, 0,  2, BOTTOM, false, app);
		loadBackground(LOGO_RITHBitmap, 0, 2, TOP, false, app);
		loadedBG = HOME;
		break;
			   }

	case MENU :{

		loadBackground(LOGO_RITHBitmap, 0, 2, TOP, false, app);
		loadBackground(MENUBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = MENU;
		break;
			   }

	case GAME :{

		loadBackground(BG_TOP_DAY_DAY_1Bitmap, BG_TOP_DAY_DAY_1BitmapLen, 2, TOP, true, app);
		loadBackground(BG_BOTTOM_DAY_1_1Bitmap, 0, 2, BOTTOM, false, app);
		loadBackground(BG_BOTTOM_DAY_1_2Bitmap, 0, 3, BOTTOM, false, app);
		loadedBG = GAME;
		break;
			   }
	case HELP :{
		loadBackground(HELP_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(HELP_BOTTOMBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = HELP;
		break;
			   }
	case TROPHIES :{
		loadBackground(SPLASH_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(TROPHIESBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = TROPHIES;
		break;
				   }
	case ARTWORKS :{
		loadBackground(SPLASH_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(ARTWORKSBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = ARTWORKS;
		break;
				   }
	case CREDITS :{
		loadBackground(SPLASH_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(CREDITSBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = CREDITS;
		break;
				  }
	case GAME_OVER :{
		loadBackground(GAME_OVER_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(GAME_OVER_BOTTOMBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = GAME_OVER;
		break;
				  }

	case LEVEL_WON :{
		loadBackground(LEVEL_WON_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(LEVEL_WON_BOTTOMBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = LEVEL_WON;
		break;
				  }
	case GAME_WON :{
		loadBackground(GAME_WON_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(GAME_WON_BOTTOMBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = GAME_WON;
		break;
				  }
	case LEVEL_PERSO_WON :{
		loadBackground(LEVEL_PERSO_WON_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(LEVEL_PERSO_WON_BOTTOMBitmap, 0, 2, BOTTOM, false, app);
		loadedBG = LEVEL_PERSO_WON;
		break;
				  }

	case STAGE_SELECT_1 :{
		loadBackground(GAME_OVER_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(STAGE_SELECT_1Bitmap, 0, 2, BOTTOM, false, app);
		loadedBG = STAGE_SELECT_1;
		break;
				  }
	case STAGE_SELECT_2 :{
		loadBackground(GAME_OVER_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(STAGE_SELECT_2Bitmap, 0, 2, BOTTOM, false, app);
		loadedBG = STAGE_SELECT_2;
		break;
				  }
	case STAGE_SELECT_3 :{
		loadBackground(GAME_OVER_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(STAGE_SELECT_3Bitmap, 0, 2, BOTTOM, false, app);
		loadedBG = STAGE_SELECT_3;
		break;
				  }

	case ARTWORK_1 :{
		loadBackground(GAME_OVER_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(ARTWORK_1Bitmap, 0, 2, BOTTOM, false, app);
		loadedBG = ARTWORK_1;
		break;
				  }
	case ARTWORK_2 :{
		loadBackground(GAME_OVER_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(ARTWORK_2Bitmap, 0, 2, BOTTOM, false, app);
		loadedBG = ARTWORK_2;
		break;
				  }
	case ARTWORK_3 :{
		loadBackground(GAME_OVER_TOPBitmap, 0, 2, TOP, false, app);
		loadBackground(ARTWORK_3Bitmap, 0, 2, BOTTOM, false, app);
		loadedBG = ARTWORK_3;
		break;
				  }
	default:
		break;

	}

	//	bgUpdate();

}

void BackgroundManager::initGameBG(Application* app){
	bgBottom2X = 0; 
	bgBottom3X = -256;
	backgroundTicks = 0;
	scrollValue = 0;
	backgroundScroll = true;
}

void BackgroundManager::scrollAll(Game* game, Application* app){
	backgroundTicks += timerElapsed(1);

	if(backgroundTicks > backgroundSpeed && backgroundScroll){
		scrollValue++;
		bgBottom2X++;
		if(bgBottom2 != 0) bgSetScroll(bgBottom2, bgBottom2X, 0);
		bgBottom3X++;
		if(bgBottom3 != 0) bgSetScroll(bgBottom3, bgBottom3X, 0);
		bgTop2X++;
		if(bgTop2 != 0) bgSetScroll(bgTop2, bgTop2X, 0);


		/////A REVOIR ///////////
		//On a une valeur BackgroundManager::backgroundScroll. Plu besoin de passer par le scroll des sprites pour le calcul. 
		//Ca evrait corrgier le pblm decran noir
		//if (backgroundScroll % (512*game->bgSpeedDif) == (256*game->bgSpeedDif)-1){
		if (scrollValue % 512 == 256-1){
			bgBottom2X=-256;
		}
		//else if (game->getScrollValue() % (512*game->bgSpeedDif) == (512*game->bgSpeedDif)-1){
		else if (scrollValue % 512 == 512-1){
			bgBottom3X=-256;
		}

		//if (game->getScrollValue() % (256*game->bgSpeedDif) == (256*game->bgSpeedDif)-1){
		if (scrollValue % 256 == 256-1){
			bgTop2X=0;
		}		

		backgroundTicks = 0;
		bgUpdate();
	}//end if
}